package com.exam.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.entity.Message;
import com.exam.entity.Replay;
import com.exam.entity.RespPageBean;
import com.exam.service.IMessageService;
import com.exam.service.IReplayService;
import com.exam.util.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 留言表 前端控制器
 * </p>
 *
 * @author zyh
 * @since 2023-03-09
 */
@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private IMessageService iMessageService;
    @Autowired
    private IReplayService iReplayService;


    @GetMapping("/{page}/{size}")
    public RespBean findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Page<Message> page1 = new Page<>(page,size);
        iMessageService.page(page1,new QueryWrapper<Message>().orderByDesc("time"));
        for (int i=0;i<page1.getRecords().size();i++){
            List<Replay> messageId = iReplayService.list(new QueryWrapper<Replay>().eq("messageId", page1.getRecords().get(i).getId()));
            page1.getRecords().get(i).setReplays(messageId);
        }
        RespPageBean respPageBean=new RespPageBean();
        respPageBean.setData(page1.getRecords());
        respPageBean.setTotal(page1.getTotal());
        return RespBean.success("请求成功！",respPageBean);
    }

    @PostMapping
    public RespBean add(@RequestBody Message message) {
        iMessageService.save(message);
        return RespBean.success("添加成功");
    }

}
